package cn.zy.company.dao;

import cn.zy.company.entity.Member;
import cn.zy.company.entity.News;
import cn.zy.company.util.DBUtil;
import cn.zy.company.util.DataSource;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class MemberDao {
    public void add(Member member) throws Exception {
        Connection connection = DataSource.getConnection();
        DBUtil.insert(connection, member);
        DataSource.closeConnection(connection);
    }

    public void delete(Integer id) throws Exception {
        Connection connection = DataSource.getConnection();
        DBUtil.delete(connection, id, Member.class);
        DataSource.closeConnection(connection);
    }

    public void update(Member member) throws Exception {
        Connection connection = DataSource.getConnection();
        DBUtil.update(connection, member);
        DataSource.closeConnection(connection);
    }

    public Member findById(Integer id) throws Exception {
        Connection connection = DataSource.getConnection();
        Member member = DBUtil.selectOne(connection, id, Member.class);
        return member;
    }

    public List<Member> findAll() throws Exception {
        Connection connection = DataSource.getConnection();
        List<Member> list = DBUtil.select(connection, "", new Object[]{}, Member.class);
        DataSource.closeConnection(connection);
        return list;
    }

    public List<Member> findAll(Integer page, Integer size) throws Exception {
        Connection connection = DataSource.getConnection();
        List<Member> list = DBUtil.select(connection, "limit ?,?", new Object[]{((page - 1) * size), size}, Member.class);
        DataSource.closeConnection(connection);
        return list;
    }
    public long countAll() throws SQLException {
        Connection connection = DataSource.getConnection();
        long count = DBUtil.count(connection, "", new Object[]{},Member.class);
        return count;
    }
}
